home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / VGX / envmap / texture.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.3 KB  |  174 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /* textures.c
  18.  * ----------
  19.  *
  20.  * $Revision: 1.2 $
  21.  *
  22.  */
  23.  
  24. #include <gl/gl.h>
  25. #include <gl/image.h>a
  26.  
  27. static int firsted;
  28.  
  29. bwtocpack(b,l,n)
  30. register unsigned short *b;
  31. register unsigned long *l;
  32. register int n;
  33. {
  34.     while(n>=8) {
  35.     l[0] = 0x00010101*b[0];
  36.     l[1] = 0x00010101*b[1];
  37.     l[2] = 0x00010101*b[2];
  38.     l[3] = 0x00010101*b[3];
  39.     l[4] = 0x00010101*b[4];
  40.     l[5] = 0x00010101*b[5];
  41.     l[6] = 0x00010101*b[6];
  42.     l[7] = 0x00010101*b[7];
  43.     l += 8;
  44.     b += 8;
  45.     n -= 8;
  46.     }
  47.     while(n--) 
  48.     *l++ = 0x00010101*(*b++);
  49. }
  50.  
  51. rgbtocpack(r,g,b,l,n)
  52. register unsigned short *r, *g, *b;
  53. register unsigned long *l;
  54. register int n;
  55. {
  56.     while(n>=8) {
  57.     l[0] = r[0] | (g[0]<<8) | (b[0]<<16);
  58.     l[1] = r[1] | (g[1]<<8) | (b[1]<<16);
  59.     l[2] = r[2] | (g[2]<<8) | (b[2]<<16);
  60.     l[3] = r[3] | (g[3]<<8) | (b[3]<<16);
  61.     l[4] = r[4] | (g[4]<<8) | (b[4]<<16);
  62.     l[5] = r[5] | (g[5]<<8) | (b[5]<<16);
  63.     l[6] = r[6] | (g[6]<<8) | (b[6]<<16);
  64.     l[7] = r[7] | (g[7]<<8) | (b[7]<<16);
  65.     l += 8;
  66.     r += 8;
  67.     g += 8;
  68.     b += 8;
  69.     n -= 8;
  70.     }
  71.     while(n--) 
  72.         *l++ = *r++ | ((*g++)<<8) | ((*b++)<<16);
  73. }
  74.  
  75. rgbatocpack(r,g,b,a,l,n)
  76. register unsigned short *r, *g, *b, *a;
  77. register unsigned long *l;
  78. register int n;
  79. {
  80.     while(n>=8) {
  81.     l[0] = r[0] | (g[0]<<8) | (b[0]<<16) | (a[0]<<24);
  82.     l[1] = r[1] | (g[1]<<8) | (b[1]<<16) | (a[1]<<24);
  83.     l[2] = r[2] | (g[2]<<8) | (b[2]<<16) | (a[2]<<24);
  84.     l[3] = r[3] | (g[3]<<8) | (b[3]<<16) | (a[3]<<24);
  85.     l[4] = r[4] | (g[4]<<8) | (b[4]<<16) | (a[4]<<24);
  86.     l[5] = r[5] | (g[5]<<8) | (b[5]<<16) | (a[5]<<24);
  87.     l[6] = r[6] | (g[6]<<8) | (b[6]<<16) | (a[6]<<24);
  88.     l[7] = r[7] | (g[7]<<8) | (b[7]<<16) | (a[7]<<24);
  89.     l += 8;
  90.     r += 8;
  91.     g += 8;
  92.     b += 8;
  93.     a += 8;
  94.     n -= 8;
  95.     }
  96.     while(n--) 
  97.         *l++ = *r++ | ((*g++)<<8) | ((*b++)<<16) | ((*a++)<<24);
  98. }
  99.  
  100. unsigned long *longimagedata(name)
  101. char *name;
  102. {
  103.     unsigned long *base, *lptr;
  104.     short *rbuf, *gbuf, *bbuf, *abuf;
  105.     IMAGE *image;
  106.     int y;
  107.  
  108.     image = iopen(name,"r");
  109.     if(!image) {
  110.     fprintf(stderr,"longimagedata: can't open image file %s\n",name);
  111.     exit(1);
  112.     }
  113.     base = (unsigned long *)malloc(image->xsize*image->ysize*sizeof(unsigned long));
  114.     rbuf = (short *)malloc(image->xsize*sizeof(short));
  115.     gbuf = (short *)malloc(image->xsize*sizeof(short));
  116.     bbuf = (short *)malloc(image->xsize*sizeof(short));
  117.     abuf = (short *)malloc(image->xsize*sizeof(short));
  118.     if(!base || !rbuf || !gbuf || !bbuf) {
  119.     fprintf(stderr,"longimagedata: can't malloc enough memory\n");
  120.     exit(1);
  121.     }
  122.     lptr = base;
  123.     for(y=0; y<image->ysize; y++) {
  124.     if(image->zsize>=4) {
  125.         getrow(image,rbuf,y,0);
  126.         getrow(image,gbuf,y,1);
  127.         getrow(image,bbuf,y,2);
  128.         getrow(image,abuf,y,3);
  129.         rgbatocpack(rbuf,gbuf,bbuf,abuf,lptr,image->xsize);
  130.         lptr += image->xsize;
  131.     } else if(image->zsize==3) {
  132.         getrow(image,rbuf,y,0);
  133.         getrow(image,gbuf,y,1);
  134.         getrow(image,bbuf,y,2);
  135.         rgbtocpack(rbuf,gbuf,bbuf,lptr,image->xsize);
  136.         lptr += image->xsize;
  137.     } else {
  138.         getrow(image,rbuf,y,0);
  139.         bwtocpack(rbuf,lptr,image->xsize);
  140.         lptr += image->xsize;
  141.     }
  142.     }
  143.     iclose(image);
  144.     free(rbuf);
  145.     free(gbuf);
  146.     free(bbuf);
  147.     free(abuf);
  148.     return base;
  149. }
  150.  
  151. textureread(name,texno)
  152. char *name;
  153. long texno;
  154. {
  155.     unsigned long *imagedata;
  156.     int i;
  157.     static float texps[] = {TX_MINFILTER,TX_MIPMAP_LINEAR,TX_MAGFILTER,TX_BILINEAR,0};
  158.     static float tevps[] = {0};
  159.  
  160.     imagedata = longimagedata(name);
  161.     texdef2d(texno, 4, 128, 128, imagedata, 0, texps);
  162.     if(!firsted) {
  163.        tevdef(1, 0, tevps);
  164.        tevbind(0,1);
  165.        firsted = 1;
  166.     }
  167. }
  168.  
  169. settexture(n)
  170. int n;
  171. {
  172.     texbind(TX_TEXTURE_0, n);
  173. }
  174.